<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
<meta name="viewport" content="width=device-width">
<meta name="theme-color" content="#222"><meta name="generator" content="Hexo 5.4.2">

  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png">
  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32x32-next.png">
  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-16x16-next.png">
  <link rel="mask-icon" href="/images/logo.svg" color="#222">

<link rel="stylesheet" href="/css/main.css">



<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css" integrity="sha256-HtsXJanqjKTc8vVQjO4YMhiqFoXkfBsjBWcX91T1jr8=" crossorigin="anonymous">
  <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/animate.css/3.1.1/animate.min.css" integrity="sha256-PR7ttpcvz8qrF57fur/yAx1qXMFJeJFiA6pSzWi0OIE=" crossorigin="anonymous">

<script class="next-config" data-name="main" type="application/json">{"hostname":"xiaoqingming18.github.io","root":"/","images":"/images","scheme":"Muse","darkmode":false,"version":"8.15.1","exturl":false,"sidebar":{"position":"left","display":"post","padding":18,"offset":12},"copycode":{"enable":false,"style":null},"bookmark":{"enable":false,"color":"#222","save":"auto"},"mediumzoom":false,"lazyload":false,"pangu":false,"comments":{"style":"tabs","active":null,"storage":true,"lazyload":false,"nav":null},"stickytabs":false,"motion":{"enable":true,"async":false,"transition":{"menu_item":"fadeInDown","post_block":"fadeIn","post_header":"fadeInDown","post_body":"fadeInDown","coll_header":"fadeInLeft","sidebar":"fadeInUp"}},"prism":false,"i18n":{"placeholder":"搜索...","empty":"没有找到任何搜索结果：${query}","hits_time":"找到 ${hits} 个搜索结果（用时 ${time} 毫秒）","hits":"找到 ${hits} 个搜索结果"}}</script><script src="/js/config.js"></script>

    <meta name="description" content="ObjectObject适合存储和在应用之间交换数据。显式地创建Object类型有两种方式，一种是使用new操作符，另一种是使用对象字面量的方式创建Object。 使用new操作符创Object123let person &#x3D; new Object();person.name &#x3D; &quot;吴大孬&quot;;person.age &#x3D; 18; 使用对象字面量创建Object1234let person &#x3D; {">
<meta property="og:type" content="article">
<meta property="og:title" content="JS：集合引用类型">
<meta property="og:url" content="http://xiaoqingming18.github.io/2023/07/02/JS%EF%BC%9A%E9%9B%86%E5%90%88%E5%BC%95%E7%94%A8%E7%B1%BB%E5%9E%8B/index.html">
<meta property="og:site_name" content="鸣蜩十七">
<meta property="og:description" content="ObjectObject适合存储和在应用之间交换数据。显式地创建Object类型有两种方式，一种是使用new操作符，另一种是使用对象字面量的方式创建Object。 使用new操作符创Object123let person &#x3D; new Object();person.name &#x3D; &quot;吴大孬&quot;;person.age &#x3D; 18; 使用对象字面量创建Object1234let person &#x3D; {">
<meta property="og:locale" content="zh_CN">
<meta property="article:published_time" content="2023-07-02T07:02:17.000Z">
<meta property="article:modified_time" content="2023-07-06T15:32:43.712Z">
<meta property="article:author" content="鸣蜩十七">
<meta property="article:tag" content="-JavaScript基础">
<meta name="twitter:card" content="summary">


<link rel="canonical" href="http://xiaoqingming18.github.io/2023/07/02/JS%EF%BC%9A%E9%9B%86%E5%90%88%E5%BC%95%E7%94%A8%E7%B1%BB%E5%9E%8B/">



<script class="next-config" data-name="page" type="application/json">{"sidebar":"","isHome":false,"isPost":true,"lang":"zh-CN","comments":true,"permalink":"http://xiaoqingming18.github.io/2023/07/02/JS%EF%BC%9A%E9%9B%86%E5%90%88%E5%BC%95%E7%94%A8%E7%B1%BB%E5%9E%8B/","path":"2023/07/02/JS：集合引用类型/","title":"JS：集合引用类型"}</script>

<script class="next-config" data-name="calendar" type="application/json">""</script>
<title>JS：集合引用类型 | 鸣蜩十七</title>
  








  <noscript>
    <link rel="stylesheet" href="/css/noscript.css">
  </noscript>

<style>.github-emoji { position: relative; display: inline-block; width: 1.2em; min-height: 1.2em; overflow: hidden; vertical-align: top; color: transparent; }  .github-emoji > span { position: relative; z-index: 10; }  .github-emoji img, .github-emoji .fancybox { margin: 0 !important; padding: 0 !important; border: none !important; outline: none !important; text-decoration: none !important; user-select: none !important; cursor: auto !important; }  .github-emoji img { height: 1.2em !important; width: 1.2em !important; position: absolute !important; left: 50% !important; top: 50% !important; transform: translate(-50%, -50%) !important; user-select: none !important; cursor: auto !important; } .github-emoji-fallback { color: inherit; } .github-emoji-fallback img { opacity: 0 !important; }</style>
</head>

<body itemscope itemtype="http://schema.org/WebPage" class="use-motion">
  <div class="headband"></div>

  <main class="main">
    <div class="column">
      <header class="header" itemscope itemtype="http://schema.org/WPHeader"><div class="site-brand-container">
  <div class="site-nav-toggle">
    <div class="toggle" aria-label="切换导航栏" role="button">
        <span class="toggle-line"></span>
        <span class="toggle-line"></span>
        <span class="toggle-line"></span>
    </div>
  </div>

  <div class="site-meta">

    <a href="/" class="brand" rel="start">
      <i class="logo-line"></i>
      <p class="site-title">鸣蜩十七</p>
      <i class="logo-line"></i>
    </a>
      <p class="site-subtitle" itemprop="description">鸣蜩十七是和女朋友谈恋爱的日子</p>
  </div>

  <div class="site-nav-right">
    <div class="toggle popup-trigger" aria-label="搜索" role="button">
    </div>
  </div>
</div>



<nav class="site-nav">
  <ul class="main-menu menu"><li class="menu-item menu-item-home"><a href="/" rel="section"><i class="fa fa-home fa-fw"></i>首页</a></li><li class="menu-item menu-item-about"><a href="/about/" rel="section"><i class="fa fa-user fa-fw"></i>关于</a></li><li class="menu-item menu-item-tags"><a href="/tags/" rel="section"><i class="fa fa-tags fa-fw"></i>标签</a></li><li class="menu-item menu-item-categories"><a href="/categories/" rel="section"><i class="fa fa-th fa-fw"></i>分类</a></li><li class="menu-item menu-item-archives"><a href="/archives/" rel="section"><i class="fa fa-archive fa-fw"></i>归档</a></li><li class="menu-item menu-item-schedule"><a href="/schedule/" rel="section"><i class="fa fa-calendar fa-fw"></i>日程表</a></li><li class="menu-item menu-item-sitemap"><a href="/sitemap.xml" rel="section"><i class="fa fa-sitemap fa-fw"></i>站点地图</a></li><li class="menu-item menu-item-commonweal"><a href="/404/" rel="section"><i class="fa fa-heartbeat fa-fw"></i>公益 404</a></li>
  </ul>
</nav>




</header>
        
  
  <aside class="sidebar">

    <div class="sidebar-inner sidebar-nav-active sidebar-toc-active">
      <ul class="sidebar-nav">
        <li class="sidebar-nav-toc">
          文章目录
        </li>
        <li class="sidebar-nav-overview">
          站点概览
        </li>
      </ul>

      <div class="sidebar-panel-container">
        <!--noindex-->
        <div class="post-toc-wrap sidebar-panel">
            <div class="post-toc animated"><ol class="nav"><li class="nav-item nav-level-1"><a class="nav-link" href="#Object"><span class="nav-text">Object</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#%E4%BD%BF%E7%94%A8new%E6%93%8D%E4%BD%9C%E7%AC%A6%E5%88%9BObject"><span class="nav-text">使用new操作符创Object</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E4%BD%BF%E7%94%A8%E5%AF%B9%E8%B1%A1%E5%AD%97%E9%9D%A2%E9%87%8F%E5%88%9B%E5%BB%BAObject"><span class="nav-text">使用对象字面量创建Object</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E4%BD%BF%E7%94%A8Object%E4%B8%BA%E6%96%B9%E6%B3%95%E4%BC%A0%E9%80%92%E5%A4%A7%E9%87%8F%E5%8F%AF%E9%80%89%E5%8F%82%E6%95%B0"><span class="nav-text">使用Object为方法传递大量可选参数</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E5%AD%98%E5%8F%96Object%E5%B1%9E%E6%80%A7"><span class="nav-text">存取Object属性</span></a></li></ol></li><li class="nav-item nav-level-1"><a class="nav-link" href="#Array"><span class="nav-text">Array</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#%E5%88%9B%E5%BB%BA%E6%95%B0%E7%BB%84"><span class="nav-text">创建数组</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#%E4%BD%BF%E7%94%A8Array%E6%9E%84%E9%80%A0%E5%87%BD%E6%95%B0%E5%88%9B%E5%BB%BA%E6%95%B0%E7%BB%84%EF%BC%9A"><span class="nav-text">使用Array构造函数创建数组：</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E4%BD%BF%E7%94%A8%E6%95%B0%E7%BB%84%E5%AD%97%E9%9D%A2%E9%87%8F%E5%88%9B%E5%BB%BA%E6%95%B0%E7%BB%84"><span class="nav-text">使用数组字面量创建数组</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#from-%E4%B8%8Eof-%E6%96%B9%E6%B3%95"><span class="nav-text">from()与of()方法</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E6%95%B0%E7%BB%84%E7%A9%BA%E4%BD%8D"><span class="nav-text">数组空位</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E6%95%B0%E7%BB%84%E7%B4%A2%E5%BC%95"><span class="nav-text">数组索引</span></a></li></ol></li></ol></div>
        </div>
        <!--/noindex-->

        <div class="site-overview-wrap sidebar-panel">
          <div class="site-author animated" itemprop="author" itemscope itemtype="http://schema.org/Person">
  <p class="site-author-name" itemprop="name">鸣蜩十七</p>
  <div class="site-description" itemprop="description">记录我的学习、生活和恋爱日常</div>
</div>
<div class="site-state-wrap animated">
  <nav class="site-state">
      <div class="site-state-item site-state-posts">
        <a href="/archives/">
          <span class="site-state-item-count">43</span>
          <span class="site-state-item-name">日志</span>
        </a>
      </div>
      <div class="site-state-item site-state-categories">
          <a href="/categories/">
        <span class="site-state-item-count">8</span>
        <span class="site-state-item-name">分类</span></a>
      </div>
      <div class="site-state-item site-state-tags">
          <a href="/tags/">
        <span class="site-state-item-count">37</span>
        <span class="site-state-item-name">标签</span></a>
      </div>
  </nav>
</div>

        </div>
      </div>
    </div>

    
  </aside>


    </div>

    <div class="main-inner post posts-expand">


  


<div class="post-block">
  
  

  <article itemscope itemtype="http://schema.org/Article" class="post-content" lang="zh-CN">
    <link itemprop="mainEntityOfPage" href="http://xiaoqingming18.github.io/2023/07/02/JS%EF%BC%9A%E9%9B%86%E5%90%88%E5%BC%95%E7%94%A8%E7%B1%BB%E5%9E%8B/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/images/avatar.gif">
      <meta itemprop="name" content="鸣蜩十七">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="鸣蜩十七">
      <meta itemprop="description" content="记录我的学习、生活和恋爱日常">
    </span>

    <span hidden itemprop="post" itemscope itemtype="http://schema.org/CreativeWork">
      <meta itemprop="name" content="JS：集合引用类型 | 鸣蜩十七">
      <meta itemprop="description" content="">
    </span>
      <header class="post-header">
        <h1 class="post-title" itemprop="name headline">
          JS：集合引用类型
        </h1>

        <div class="post-meta-container">
          <div class="post-meta">
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-calendar"></i>
      </span>
      <span class="post-meta-item-text">发表于</span>

      <time title="创建时间：2023-07-02 15:02:17" itemprop="dateCreated datePublished" datetime="2023-07-02T15:02:17+08:00">2023-07-02</time>
    </span>
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-calendar-check"></i>
      </span>
      <span class="post-meta-item-text">更新于</span>
      <time title="修改时间：2023-07-06 23:32:43" itemprop="dateModified" datetime="2023-07-06T23:32:43+08:00">2023-07-06</time>
    </span>
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-folder"></i>
      </span>
      <span class="post-meta-item-text">分类于</span>
        <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
          <a href="/categories/%E5%89%8D%E7%AB%AF/" itemprop="url" rel="index"><span itemprop="name">-前端</span></a>
        </span>
    </span>

  
</div>

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">
        <h1 id="Object"><a href="#Object" class="headerlink" title="Object"></a>Object</h1><p><code>Object</code>适合存储和在应用之间交换数据。显式地创建<code>Object</code>类型有两种方式，一种是使用<code>new</code>操作符，另一种是使用<code>对象字面量</code>的方式创建<code>Object</code>。</p>
<h2 id="使用new操作符创Object"><a href="#使用new操作符创Object" class="headerlink" title="使用new操作符创Object"></a>使用<code>new</code>操作符创<code>Object</code></h2><figure class="highlight javascript"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">let</span> person = <span class="keyword">new</span> <span class="title class_">Object</span>();</span><br><span class="line">person.<span class="property">name</span> = <span class="string">"吴大孬"</span>;</span><br><span class="line">person.<span class="property">age</span> = <span class="number">18</span>;</span><br></pre></td></tr></tbody></table></figure>
<h2 id="使用对象字面量创建Object"><a href="#使用对象字面量创建Object" class="headerlink" title="使用对象字面量创建Object"></a>使用对象字面量创建<code>Object</code></h2><figure class="highlight javascript"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">let</span> person = {</span><br><span class="line">    <span class="attr">name</span>: <span class="string">"吴大孬"</span>,</span><br><span class="line">    <span class="attr">age</span>: <span class="number">18</span></span><br><span class="line">};</span><br></pre></td></tr></tbody></table></figure>
<p>也可以先创建一个空的<code>Object</code>，随后添加属性与值：</p>
<figure class="highlight javascript"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">let</span> person = {};</span><br><span class="line">person.<span class="property">name</span> = <span class="string">"吴大孬"</span>;</span><br><span class="line">person.<span class="property">age</span> = <span class="number">18</span>;</span><br></pre></td></tr></tbody></table></figure>
<p>使用对象字面量创建对象时要注意以下几点：</p>
<ul>
<li>属性之间用逗号隔开，最后一个属性不要加逗号。</li>
<li>属性名可以是字符串或数值。</li>
<li>使用对象字面量创<code>Object</code>时，不会调用<code>Object</code>构造函数。</li>
</ul>
<h2 id="使用Object为方法传递大量可选参数"><a href="#使用Object为方法传递大量可选参数" class="headerlink" title="使用Object为方法传递大量可选参数"></a>使用Object为方法传递大量可选参数</h2><figure class="highlight javascript"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">function</span> <span class="title function_">displayInfo</span>(<span class="params">args</span>) {</span><br><span class="line">    <span class="keyword">let</span> output = <span class="string">""</span>;</span><br><span class="line">    <span class="keyword">if</span>(<span class="keyword">typeof</span> args.<span class="property">name</span> == <span class="string">"string"</span>) {</span><br><span class="line">        output += <span class="string">"Name: "</span> + args.<span class="property">name</span> + <span class="string">"\n"</span>;</span><br><span class="line">    }</span><br><span class="line">    <span class="keyword">if</span>(<span class="keyword">typeof</span> args.<span class="property">age</span> == <span class="string">"number"</span>) {</span><br><span class="line">        output += <span class="string">"Age: "</span> + args.<span class="property">age</span> + <span class="string">"\n"</span>;</span><br><span class="line">    }</span><br><span class="line">    <span class="title function_">alert</span>(output);</span><br><span class="line">}</span><br><span class="line"></span><br><span class="line"><span class="title function_">displayInfo</span>({</span><br><span class="line">    <span class="attr">name</span>: <span class="string">"吴大孬"</span>,</span><br><span class="line">    <span class="attr">age</span>: <span class="number">18</span></span><br><span class="line">});</span><br></pre></td></tr></tbody></table></figure>
<h2 id="存取Object属性"><a href="#存取Object属性" class="headerlink" title="存取Object属性"></a>存取<code>Object</code>属性</h2><p>最常用的是通过<strong>点语法</strong>存取<code>Object</code>的属性，也可以通过中括号来存取<code>Object</code>属性，中括号内使用属性名的字符串形式，例如：</p>
<figure class="highlight javascript"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(person[<span class="string">"name"</span>]);</span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(person.<span class="property">name</span>);</span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(person[<span class="string">"first name"</span>])</span><br></pre></td></tr></tbody></table></figure>
<h1 id="Array"><a href="#Array" class="headerlink" title="Array"></a>Array</h1><p>JavaScript中的数组与其他语言中的数组有很大的区别。JavaScript中的数组可以存不同数据类型的数据，并且数组是动态大小的，会随着数据添加而自动增长。</p>
<h2 id="创建数组"><a href="#创建数组" class="headerlink" title="创建数组"></a>创建数组</h2><p>有几种方式可以创建数组，下面一一介绍。</p>
<h3 id="使用Array构造函数创建数组："><a href="#使用Array构造函数创建数组：" class="headerlink" title="使用Array构造函数创建数组："></a>使用Array构造函数创建数组：</h3><figure class="highlight javascript"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">let</span> colors = <span class="keyword">new</span> <span class="title class_">Array</span>();</span><br></pre></td></tr></tbody></table></figure>
<p>还可以给构造函数传入一个数值，创建指初始容量的数组：</p>
<figure class="highlight javascript"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">let</span> colors = <span class="keyword">new</span> <span class="title class_">Array</span>(<span class="number">20</span>);</span><br></pre></td></tr></tbody></table></figure>
<p>也可以给构造函数传入要插入到数组中的元素：</p>
<figure class="highlight javascript"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">let</span> colors = <span class="keyword">new</span> <span class="title class_">Array</span>(<span class="string">"red"</span>,<span class="string">"blue"</span>,<span class="string">"green"</span>);</span><br></pre></td></tr></tbody></table></figure>
<p>也可以省略<code>new</code>操作符：</p>
<figure class="highlight javascript"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">let</span> colors = <span class="title class_">Array</span>(<span class="number">3</span>);</span><br><span class="line"><span class="keyword">let</span> names = <span class="title class_">Array</span>(<span class="string">"吴大孬"</span>,<span class="string">"二妹"</span>);</span><br></pre></td></tr></tbody></table></figure>
<h3 id="使用数组字面量创建数组"><a href="#使用数组字面量创建数组" class="headerlink" title="使用数组字面量创建数组"></a>使用数组字面量创建数组</h3><p>可以像下面这样用数组字面量创建数组：</p>
<figure class="highlight javascript"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">let</span> colors = [<span class="string">"red"</span>,<span class="string">"green"</span>,<span class="string">"white"</span>];</span><br></pre></td></tr></tbody></table></figure>
<h3 id="from-与of-方法"><a href="#from-与of-方法" class="headerlink" title="from()与of()方法"></a><code>from()</code>与<code>of()</code>方法</h3><p><code>from()</code>和<code>of()</code>都是用来创建数组的静态方法。<code>from()</code>用于将类数组结构转化为数组实例，<code>of()</code>方法用于将一组参数转换为数组实例。</p>
<p><code>Array.from()</code> 方法用于从一个类似数组或可迭代对象中创建一个新的数组实例。它接收一个可迭代对象（如字符串、类数组对象或 Set）或一个类似数组的对象，并将其转换为一个真正的数组。这个方法可以接收一个可选的映射函数和一个可选的上下文对象，用于对原始元素进行映射或调整。</p>
<p>下面是一个使用 <code>Array.from()</code> 方法的示例：</p>
<figure class="highlight javascript"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 字符串转换为数组</span></span><br><span class="line"><span class="keyword">const</span> str = <span class="string">'Hello'</span>;</span><br><span class="line"><span class="keyword">const</span> arr = <span class="title class_">Array</span>.<span class="title function_">from</span>(str);</span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(arr); <span class="comment">// ['H', 'e', 'l', 'l', 'o']</span></span><br><span class="line"></span><br><span class="line"><span class="comment">// 可迭代对象转换为数组</span></span><br><span class="line"><span class="keyword">const</span> set = <span class="keyword">new</span> <span class="title class_">Set</span>([<span class="string">'a'</span>, <span class="string">'b'</span>, <span class="string">'c'</span>]);</span><br><span class="line"><span class="keyword">const</span> arr2 = <span class="title class_">Array</span>.<span class="title function_">from</span>(set);</span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(arr2); <span class="comment">// ['a', 'b', 'c']</span></span><br><span class="line"></span><br><span class="line"><span class="comment">// 使用映射函数</span></span><br><span class="line"><span class="keyword">const</span> numbers = [<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>, <span class="number">4</span>, <span class="number">5</span>];</span><br><span class="line"><span class="keyword">const</span> doubled = <span class="title class_">Array</span>.<span class="title function_">from</span>(numbers, <span class="function"><span class="params">num</span> =&gt;</span> num * <span class="number">2</span>);</span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(doubled); <span class="comment">// [2, 4, 6, 8, 10]</span></span><br></pre></td></tr></tbody></table></figure>
<p><code>Array.of()</code> 方法用于创建一个包含任意数量参数的新数组。不同于 <code>Array</code> 构造函数，<code>Array.of()</code> 方法会将传递的参数作为数组的元素，而不是将它们视为单个参数或数组长度。这对于创建具有确定元素的新数组很有用。</p>
<p>下面是一个使用 <code>Array.of()</code> 方法的示例：</p>
<figure class="highlight javascript"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> arr1 = <span class="title class_">Array</span>.<span class="title function_">of</span>(<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>, <span class="number">4</span>, <span class="number">5</span>);</span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(arr1); <span class="comment">// [1, 2, 3, 4, 5]</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">const</span> arr2 = <span class="title class_">Array</span>.<span class="title function_">of</span>(<span class="string">'a'</span>, <span class="string">'b'</span>, <span class="string">'c'</span>);</span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(arr2); <span class="comment">// ['a', 'b', 'c']</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">const</span> arr3 = <span class="title class_">Array</span>.<span class="title function_">of</span>(<span class="number">1</span>, <span class="string">'two'</span>, [<span class="number">3</span>, <span class="number">4</span>], { <span class="attr">five</span>: <span class="number">5</span> });</span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(arr3); <span class="comment">// [1, 'two', [3, 4], { five: 5 }]</span></span><br></pre></td></tr></tbody></table></figure>
<h2 id="数组空位"><a href="#数组空位" class="headerlink" title="数组空位"></a>数组空位</h2><p>JavaScript 数组空位指的是数组中某些位置上没有任何值的情况。空位在数组中是一种特殊的值，表示该位置上缺少元素。</p>
<p><strong>1. 访问和遍历空位：</strong> 当数组中存在空位时，使用索引访问空位时将返回 <code>undefined</code>。例如：</p>
<figure class="highlight javascript"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> arr = [<span class="number">1</span>, , <span class="number">3</span>];</span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(arr[<span class="number">1</span>]); <span class="comment">// undefined</span></span><br></pre></td></tr></tbody></table></figure>
<p>在使用 <code>for...in</code> 或 <code>for...of</code> 循环遍历数组时，空位会被跳过。例如：</p>
<figure class="highlight javascript"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> arr = [<span class="number">1</span>, , <span class="number">3</span>];</span><br><span class="line"><span class="keyword">for</span> (<span class="keyword">let</span> item <span class="keyword">of</span> arr) {</span><br><span class="line">  <span class="variable language_">console</span>.<span class="title function_">log</span>(item); <span class="comment">// 1, 3</span></span><br><span class="line">}</span><br></pre></td></tr></tbody></table></figure>
<p><strong>2. 数组方法对待空位的不同：</strong> 不同的数组方法在处理空位时可能会表现出不同的行为。</p>
<ul>
<li><code>forEach()</code>、<code>map()</code>、<code>filter()</code> 和 <code>reduce()</code> 等迭代方法会跳过空位。</li>
<li><code>join()</code> 和 <code>toString()</code> 方法会将空位视为空字符串。</li>
<li><code>indexOf()</code>、<code>find()</code>、<code>findIndex()</code> 等查找方法无法找到空位。</li>
<li><code>push()</code> 方法会将空位视为有效元素，将其添加到数组末尾。</li>
<li><code>slice()</code> 方法会跳过空位，返回一个新数组。</li>
<li><code>sort()</code> 方法会将空位视为 undefined，并根据 undefined 的规则进行排序。</li>
</ul>
<p>下面是一些示例：</p>
<figure class="highlight javascript"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> arr = [<span class="number">1</span>, , <span class="number">3</span>];</span><br><span class="line"></span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(arr.<span class="property">forEach</span>); <span class="comment">// [Function: forEach]</span></span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(arr.<span class="property">map</span>); <span class="comment">// [Function: map]</span></span><br><span class="line"></span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(arr.<span class="title function_">join</span>()); <span class="comment">// "1,,3"</span></span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(arr.<span class="title function_">toString</span>()); <span class="comment">// "1,,3"</span></span><br><span class="line"></span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(arr.<span class="title function_">indexOf</span>(<span class="literal">undefined</span>)); <span class="comment">// -1</span></span><br><span class="line"></span><br><span class="line">arr.<span class="title function_">push</span>(<span class="number">4</span>);</span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(arr); <span class="comment">// [1, , 3, 4]</span></span><br><span class="line"></span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(arr.<span class="title function_">slice</span>()); <span class="comment">// [1, , 3, 4]</span></span><br><span class="line"></span><br><span class="line">arr.<span class="title function_">sort</span>();</span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(arr); <span class="comment">// [1, 3, 4, undefined]</span></span><br></pre></td></tr></tbody></table></figure>
<p><strong>3. 删除空位：</strong> 要从数组中删除空位，可以使用 <code>delete</code> 操作符或 <code>Array.prototype.filter()</code> 方法。例如：</p>
<figure class="highlight javascript"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> arr = [<span class="number">1</span>, , <span class="number">3</span>];</span><br><span class="line"><span class="keyword">delete</span> arr[<span class="number">1</span>];</span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(arr); <span class="comment">// [1, empty, 3]</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">const</span> filteredArr = arr.<span class="title function_">filter</span>(<span class="function">() =&gt;</span> <span class="literal">true</span>);</span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(filteredArr); <span class="comment">// [1, 3]</span></span><br></pre></td></tr></tbody></table></figure>
<p>需要注意的是，使用 <code>delete</code> 操作符删除空位后，数组的长度不会改变，仍然保持原来的长度。</p>
<p>尽管 JavaScript 允许数组中存在空位，但在大多数情况下，建议避免使用空位，以避免产生意外的行为。</p>
<h2 id="数组索引"><a href="#数组索引" class="headerlink" title="数组索引"></a>数组索引</h2><p>与其他语言中的数组不同，JavaScript数组若使用超过范围的索引访问数组，数组长度会自动扩展到该索引值加1。</p>
<p>数组中元素的数量保存在数组的<code>length</code>属性中：</p>
<figure class="highlight javascript"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">let</span> colors = [<span class="string">"red"</span>,<span class="string">"green"</span>,<span class="string">"white"</span>];</span><br><span class="line"><span class="title function_">alert</span>(colors.<span class="property">length</span>); <span class="comment">// 3</span></span><br></pre></td></tr></tbody></table></figure>

    </div>

    
    
    

    <footer class="post-footer">
          <div class="post-tags">
              <a href="/tags/JavaScript%E5%9F%BA%E7%A1%80/" rel="tag"># -JavaScript基础</a>
          </div>

        

          <div class="post-nav">
            <div class="post-nav-item">
                <a href="/2023/06/28/Java%EF%BC%9A%E5%B8%B8%E7%94%A8%E5%AE%9E%E7%94%A8%E7%B1%BB/" rel="prev" title="">
                  <i class="fa fa-chevron-left"></i> 
                </a>
            </div>
            <div class="post-nav-item">
                <a href="/2023/07/10/%E5%8F%98%E9%87%8F%E3%80%81%E4%BD%9C%E7%94%A8%E5%9F%9F%E5%92%8C%E5%86%85%E5%AD%98/" rel="next" title="">
                   <i class="fa fa-chevron-right"></i>
                </a>
            </div>
          </div>
    </footer>
  </article>
</div>






</div>
  </main>

  <footer class="footer">
    <div class="footer-inner">


<div class="copyright">
  &copy; 
  <span itemprop="copyrightYear">2023</span>
  <span class="with-love">
    <i class="fa fa-heart"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">鸣蜩十七</span>
</div>
  <div class="powered-by">由 <a href="https://hexo.io/" rel="noopener" target="_blank">Hexo</a> & <a href="https://theme-next.js.org/muse/" rel="noopener" target="_blank">NexT.Muse</a> 强力驱动
  </div>

    </div>
  </footer>

  
  <div class="toggle sidebar-toggle" role="button">
    <span class="toggle-line"></span>
    <span class="toggle-line"></span>
    <span class="toggle-line"></span>
  </div>
  <div class="sidebar-dimmer"></div>
  <div class="back-to-top" role="button" aria-label="返回顶部">
    <i class="fa fa-arrow-up fa-lg"></i>
    <span>0%</span>
  </div>

<noscript>
  <div class="noscript-warning">Theme NexT works best with JavaScript enabled</div>
</noscript>


  
  <script src="https://cdnjs.cloudflare.com/ajax/libs/animejs/3.2.1/anime.min.js" integrity="sha256-XL2inqUJaslATFnHdJOi9GfQ60on8Wx1C2H8DYiN1xY=" crossorigin="anonymous"></script>
<script src="/js/comments.js"></script><script src="/js/utils.js"></script><script src="/js/motion.js"></script><script src="/js/schemes/muse.js"></script><script src="/js/next-boot.js"></script>

  




  




  

  <script class="next-config" data-name="enableMath" type="application/json">true</script><script class="next-config" data-name="mathjax" type="application/json">{"enable":true,"tags":"none","js":{"url":"https://cdnjs.cloudflare.com/ajax/libs/mathjax/3.2.2/es5/tex-mml-chtml.js","integrity":"sha256-MASABpB4tYktI2Oitl4t+78w/lyA+D7b/s9GEP0JOGI="}}</script>
<script src="/js/third-party/math/mathjax.js"></script>



</body>
</html>
